package so.shanku.essential.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import aym.util.json.JsonMap;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnItemClick;
import com.nostra13.universalimageloader.core.ImageLoader;
import so.shanku.essential.R;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.utils.ImageLoaderBitmapCallBack;
import striveen.util.used.view.view.HorizontalListView;

import java.util.ArrayList;

/**
 * Created by Jaelyn on 2015/9/6 0006.
 */
public class FindProductAdapter extends MyBaseAdapter {

	/**
	 * 按钮和推荐的列表的点击事件
	 */
	private ItemClickListener itemClickListener;


	public void setItemClickListener(ItemClickListener itemClickListener) {
		this.itemClickListener = itemClickListener;
	}

	public FindProductAdapter(Context context) {
		super(context);
	}

	@Override
	public View getView(int i, View view, ViewGroup viewGroup) {
		FindProductAdapterViewHolder findProductAdapterViewHolder;
		if (view == null) {
			view = layoutInflater.inflate(R.layout.findproduct_fragment_layout_item, null);
			findProductAdapterViewHolder = new FindProductAdapterViewHolder();
			ViewUtils.inject(findProductAdapterViewHolder, view);
			view.setTag(findProductAdapterViewHolder);
		} else {
			findProductAdapterViewHolder = (FindProductAdapterViewHolder) view.getTag();
		}
		setItemTag(i,findProductAdapterViewHolder);
		bindData(i, findProductAdapterViewHolder);
		return view;
	}

	private void setItemTag(int i, FindProductAdapterViewHolder findProductAdapterViewHolder) {
		findProductAdapterViewHolder.find_page_item_hv.setTag(i);
	}

	private void bindData(final int i, FindProductAdapterViewHolder viewHolder) {
		JsonMap jsonMap= (JsonMap) datas.get(i);
		ArrayList<JsonMap<String,Object>> recommandGoodsList= (ArrayList<JsonMap<String, Object>>) jsonMap.getList_JsonMap("ProductList");
		RecommandProductAdapter recommandProductAdapter = new RecommandProductAdapter(context);
		recommandProductAdapter.setType(1);
		recommandProductAdapter.setDatas(recommandGoodsList);
		viewHolder.find_page_item_hv.setAdapter(recommandProductAdapter);
		viewHolder.find_item_big_title_tv.setText(jsonMap.getStringNoNull("Title"));
		String picPath=jsonMap.getStringNoNull("MainImage");
		if(MyApplication.getInstance().getBitmapHashMap().containsKey(picPath)) {
			viewHolder.big_iv.setImageBitmap(MyApplication.getInstance().getBitmapHashMap().get(picPath));
		} else {
			ImageLoader.getInstance().displayImage(picPath,viewHolder.big_iv,MyApplication.getInstance().getDefaultOption(),new ImageLoaderBitmapCallBack());
		}
	}


	public class FindProductAdapterViewHolder {
		@ViewInject(R.id.big_iv)
		public ImageView big_iv;

		@ViewInject(R.id.find_item_big_title_tv)
		public TextView find_item_big_title_tv;

		@ViewInject(R.id.find_item_classfy_tv)
		public TextView find_item_classfy_tv;

        @ViewInject(R.id.create_time_tv)
        public TextView find_item_create_time_tv;

        @ViewInject(R.id.find_item_scan_num_tv)
        public TextView find_item_scan_num_tv;

//		@OnClick(R.id.find_item_more_tv)
//		public void find_item_more_tv(View view) {
//			if (itemClickListener != null) {
//				itemClickListener.onItemClickListener(Constant.FindItemCompontType.MORE_CLICK, (Integer) view.getTag(), -1);
//			}
//		}

		@ViewInject(R.id.find_page_item_hv)
		public HorizontalListView find_page_item_hv;

		@OnItemClick(R.id.find_page_item_hv)
		public void find_page_item_hv_ItemClick(AdapterView<?> parent, View view, int position, long id) {
			if (itemClickListener != null) {
				//正式调接口的时候需要更具需要进行修改参数data
				itemClickListener.onItemClickListener(Constant.FindItemCompontType.RECOMMANDOODS, (Integer) find_page_item_hv.getTag(), position);
			}
		}
	}

	public interface ItemClickListener {
		/**
		 * 返回item的空间的点击事件
		 *
		 * @param itemCompontType          点击事件的类型 RECOMMAND_ITEM_CLICK：推荐列表的点击事件  MORE_CLICK：更多点击
		 * @param position      点击的item的位置
		 * @param childPosition 当点击的是推荐列表的item 返回数据的子list的位置 否则返回-1
		 */
		public void onItemClickListener(Constant.FindItemCompontType itemCompontType, int position, int childPosition);
	}
}
